package net.jdm.constraint;

import java.util.HashSet;
import java.util.Set;

import net.jdm.constraint.cycle.CycleConstraint;
import net.jdm.constraint.dependence.DependenceConstraint;
import net.jdm.constraint.distance.DistanceConstraint;

import net.jdm.constraint.instability.InstabilityDependConstraint;
import net.jdm.domain.entity.Component;
import net.jdm.domain.entity.SystemModel;

public class ComponentsConstraint {
	private DistanceConstraint distanceConstraint;
	private CycleConstraint componentCycleConstraint;
	private InstabilityDependConstraint componentInstabilityConstraint;
	private Set<DependenceConstraint> componentDependencyConstraints= new HashSet<DependenceConstraint>();

    public boolean check(SystemModel systemModel){
    	boolean pass=true;
    	for(DependenceConstraint constraint:componentDependencyConstraints){
    		pass=constraint.check(systemModel)&&pass;
    	}
    	if(componentCycleConstraint!=null)
    		pass=componentCycleConstraint.check(systemModel)&&pass;
    	if(distanceConstraint!=null)
    		pass=checkAllComponentsDistance(systemModel.getComponents())&&pass;
		if(componentInstabilityConstraint!=null){
			pass=componentInstabilityConstraint.check(systemModel)&&pass;
		}
    	return pass;
    }
    public boolean checkConstraint(Component actualComponent){
    	return distanceConstraint.check(actualComponent);
    }
	public void setComponentDependencyConstraints(Set<DependenceConstraint> componentDependencyConstraints) {
		this.componentDependencyConstraints = componentDependencyConstraints;
	}

	public Set<DependenceConstraint> getComponentDependencyConstraints() {
		return componentDependencyConstraints;
	}

	public void setDistanceConstraint(DistanceConstraint distanceConstraint) {
		this.distanceConstraint = distanceConstraint;
	}

	public DistanceConstraint getDistanceConstraint() {
		return distanceConstraint;
	}

	public void setComponentCycleConstraint(CycleConstraint componentCycleConstraint) {
		this.componentCycleConstraint = componentCycleConstraint;
	}

	public CycleConstraint getComponentCycleConstraint() {
		return componentCycleConstraint;
	}
	public void setComponentInstabilityConstraint(
			InstabilityDependConstraint componentInstabilityConstraint) {
		this.componentInstabilityConstraint = componentInstabilityConstraint;
	}
	public InstabilityDependConstraint getComponentInstabilityConstraint() {
		return componentInstabilityConstraint;
	}
	public boolean checkAllComponentsDistance(Set<Component> actualComponents) {
		boolean pass=true;
		for(Component component:actualComponents){
			pass=distanceConstraint.check(component)&&pass;
		}
		return pass;
	}
}
